/** @file    FormMediaDirector.cpp
 *  @author  Alessandro Polo
 *  @version $Id: FormMediaDirector.cpp 2748 2010-07-11 22:07:06Z alex $
 *  @brief
 * File containing methods for the wosh::gui::FormMediaDirector class.
 * The header for this class can be found in FormMediaDirector.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "FormMediaDirector.h"
 #include "ui_FormMediaDirector.h"

 #include <core/UserManager.h>
 #include <core/WoshKernel.h>
 #include <core/MethodsCommon.h>
 #include <core/Request.h>
 #include <core/Response.h>

 #include <framework/gui/WidgetMediaZone.h>

 #include <QtGui>
 #include <framework/gui/GuiUtilities.h>


using namespace std;
namespace wosh {
 namespace gui {

 WOSH_REGISTER(wosh::gui::FormMediaDirector, "wosh::gui::IWidgetGeneric", 1.0, _static_FormMediaDirector )
 WOSH_GUI_MAP(wosh::gui::FormMediaDirector, "wosh::interfaces::services::MediaDirector", 0.0, _static_Map_FormMediaDirector )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

FormMediaDirector::FormMediaDirector(QWidget *parent) : IWidgetGeneric(parent)
 {
	this->m_ui = new Ui::FormMediaDirector();
	this->m_ui->setupUi(this);

	this->setWindowTitle("Media Director");
	this->getWidgetURI().setName( Utilities::format("FormMediaDirector#%10lu", Utilities::randomLong()) );
	this->getTargetURI().clearPathName();
	this->getTargetURI().setName("Media");

	buildMenus();

	this->m_ui->tableViewMatrix->setMethodList(_MediaDirector_METHOD_get_matrix);
	this->m_ui->tableViewMatrix->getTableView()->setContextMenuPolicy(Qt::CustomContextMenu);
	this->m_ui->tableViewMatrix->getTableView()->verticalHeader()->setVisible(true);
	this->m_ui->tableViewMatrix->setResizeStretch(2);
	connect(this->m_ui->tableViewMatrix, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );
	connect(this->m_ui->tableViewMatrix, SIGNAL(tableContextMenuRequested(const QPoint&, const QModelIndex&)),this, SLOT(tableViewMatrix_ContextMenu(const QPoint&, const QModelIndex&)) );
	connect(this->m_ui->tableViewMatrix->getTableView(), SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(tableViewMatrix_doubleClicked(const QModelIndex&)));

	connect(this->m_ui->tableViewZones, SIGNAL(updated()), this->m_ui->wUpBarZones, SLOT(sayReady()));
	connect(this->m_ui->tableViewZones, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(tableViewZones_doubleClicked(const QModelIndex&)));
	connect(this->m_ui->wUpBarZones, SIGNAL(updateClicked()), this, SLOT(refreshZones()));
	connect(this->m_ui->wUpBarZones2, SIGNAL(updateClicked()), this, SLOT(refreshZones()));

	this->m_ui->tableViewPlaylists->setMethodList(_MediaDirector_METHOD_list_playlists);
	connect(this->m_ui->tableViewPlaylists, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );
	connect(this->m_ui->tableViewPlaylists->getTableView(), SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(tableViewPlaylists_doubleClicked(const QModelIndex&)));
	connect(this->m_ui->btnMatrixPlay, SIGNAL(clicked()), this, SLOT(btnMatrixPlay_clicked()));

	mapMethodButton(_MediaDirector_METHOD_forceupdate, this->m_ui->btnForceUpdate);

 }

FormMediaDirector::~FormMediaDirector()
 {
	delete this->m_ui;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormMediaDirector::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormMediaDirector::refreshAll() {
	this->setWindowTitle( QString("Media Director [%1]").arg(this->getTargetURI().getKernelName().c_str()) );
	refreshZones();
	this->m_ui->tableViewMatrix->refreshData();
	this->m_ui->tableViewPlaylists->refreshData();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormMediaDirector::refreshZones() {
	executeRemoteMethod( _METHOD_List );
	this->m_ui->wUpBarZones->sayRetrieving();
	this->m_ui->wUpBarZones2->sayRetrieving();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormMediaDirector::btnMatrixPlay_clicked() {
	play_( this->m_ui->tableViewMatrix->getTableView()->currentIndex() );
 }

void FormMediaDirector::play_(const QModelIndex& index ) {
	string zone; string playlist;
	getMatrixInfo(  zone, playlist, index );
	List* dl = new List();
	dl->push_back<Variant>( zone );
	dl->push_back<Variant>( playlist );
	executeRemoteMethod( _MediaDirector_METHOD_play, dl );
 }

void FormMediaDirector::stop_(const QModelIndex& index ) {
	string zone; string playlist;
	getMatrixInfo(  zone, playlist, index );
	List* dl = new List();
	dl->push_back<Variant>( zone );
	dl->push_back<Variant>( playlist );
	executeRemoteMethod( _MediaDirector_METHOD_stop, dl );
 }

void FormMediaDirector::getMatrixInfo( string& zone, string& playlist, const QModelIndex& index ) {
	if ( !index.isValid() ) return;
	zone = this->m_ui->tableViewMatrix->getTableView()->getItemAsVariant(-1, index.column()).toString();
	playlist = this->m_ui->tableViewMatrix->getTableView()->getItemAsVariant(index.row(), -1).toString();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormMediaDirector::tableViewMatrix_doubleClicked( const QModelIndex& index ) {
	string zone; string playlist;
	getMatrixInfo( zone, playlist, index );
	List* dl = new List();
	dl->push_back<Variant>( zone );
	dl->push_back<Variant>( playlist );
	executeRemoteMethod( _MediaDirector_METHOD_list_join, dl );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormMediaDirector::tableViewZones_doubleClicked( const QModelIndex& ) {
	Variant dataUri = this->m_ui->tableViewZones->getCurrentItemAsVariantAtCol(0);

	WidgetMediaZone* mZone = new WidgetMediaZone();
	mZone->setTargetURI( dataUri.toString() );
	mZone->refreshAll();
	mZone->show();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormMediaDirector::tableViewPlaylists_doubleClicked( const QModelIndex& ) {
	Variant dataName = this->m_ui->tableViewPlaylists->getCurrentItemAsVariantAtCol(0);

	URI mList_URI;
	mList_URI.setParent( this->getTargetURI() );
	mList_URI.getPath().push_back("Playlist");
	mList_URI.setName( dataName.toString() );

	WidgetMediaList* mList = new WidgetMediaList();
	mList->setTargetURI( mList_URI );
	mList->refreshAll();
	mList->show();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormMediaDirector::buildMenus() {
	this->actionMatrixTitle = new QAction(this);
	this->actionMatrixTitle->setEnabled(false);

	this->actionMatrixPlay = new QAction(this);
	this->actionMatrixPlay->setText(tr("&Play.."));
	this->actionMatrixMove = new QAction(this);
	this->actionMatrixMove->setText(tr("&Move here.."));
	this->actionMatrixStop = new QAction(this);
	this->actionMatrixStop->setText(tr("&Stop.."));
	this->actionMatrixStopOther = new QAction(this);
	this->actionMatrixStopOther->setText(tr("&Stop others.."));

	this->menuMatrix = new QMenu(this);
	this->menuMatrix->addAction(this->actionMatrixTitle);
	this->menuMatrix->addSeparator();
	this->menuMatrix->addAction(this->actionMatrixPlay);
	this->menuMatrix->addAction(this->actionMatrixMove);
	this->menuMatrix->addSeparator();
	this->menuMatrix->addAction(this->actionMatrixStop);
	this->menuMatrix->addSeparator();
	this->menuMatrix->addAction(this->actionMatrixStopOther);
/*
- play 
- move here
- stop
- stop others

*/
 }

void FormMediaDirector::tableViewMatrix_ContextMenu( const QPoint& pos, const QModelIndex& index ) {
	QPoint globalPos = this->mapToGlobal(pos);
	if ( !index.isValid() ) return;
	string zone;
	string playlist;
	getMatrixInfo( zone, playlist, index );
	this->actionMatrixTitle->setText( QString("%1->%2").arg(zone.c_str()).arg(playlist.c_str()) );
	QAction* selected = this->menuMatrix->exec( globalPos );
	if ( selected == this->actionMatrixPlay )
		play_(index);
	else if ( selected == this->actionMatrixStop )
		stop_(index);

//	else if ( selected == this->actionMatrixMove )
//	else if ( selected == this->actionMatrixStop )
//	else if ( selected == this->actionMatrixStopOther )

 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormMediaDirector::busMessage( const Message& message, const Bus* )
 {
	if ( !isMessageForMeOrFromTarget(message) )
		return;

	if ( message.getContent()->isResponse() ) {
		const wosh::Response* response = message.getContent()->asResponse();

		this->m_ui->tableViewMatrix->evalResponse(response);
		this->m_ui->tableViewZones->evalResponse(response);
		this->m_ui->tableViewPlaylists->evalResponse(response);
		promptMethodResponse(message);
	 }
 }


//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
